#include "imagereader.h"
#include "common/stringutils.h"
#include "common/symbol.h"
#include "common/masking.h"
#include "generate/imagecreator.h"

#include <QSize>
#include <QColor>

#include <QDebug>

namespace qrcode {


ImageReader::ImageReader()
{

}

void ImageReader::ReadBits()
{
    //当前版本被锁定的模块，即非数据模块
    ImageCreator *pCreator = new ImageCreator();
    lockModules =  pCreator->GetLockModule(version);

    //消除掩模
    Masking *pmask = new Masking(version);
    pmask->setMaskNum(maskNum);
    pmask->setLockModule(lockModules);
    pmask->RemoveMask(image);

    //读取数据位流
    databitstream = ReadDataBits();
}

void ImageReader::ReadFormatVertionBits()
{
    QSize size = image.size();
    nMods = size.width();
    version = (nMods - 21)/4 + 1;

    StringUtils::PRINT("初始每边模块数：", nMods);
    StringUtils::PRINT("初始版本号：", version);

    //读取数据位流
    formatbits = ReadFormatBits();

    if(version >= 7){
        versionbits = ReadVersionBits();
    }
}

void ImageReader::setMaskNum(int value)
{
    maskNum = value;
}

std::vector<int> ImageReader::ReadDataBits()
{
    std::vector<int> bits;
    int n = 0;
    //右侧区域
    for(int i = 1; i < 9; i += 2){
        //向上填充
        for(int j = 1; j <= nMods-9; j++){
            if(!IsLockModule(nMods-i, nMods-j)){
                bits.push_back(PixelValue(nMods-i, nMods-j));
                n++;
            }
            if(!IsLockModule(nMods-i-1, nMods-j)){
                bits.push_back(PixelValue(nMods-i-1, nMods-j));
                n++;
            }
        }
        //向下填充
        i += 2;
        for(int j = 9; j <= nMods-1; j++){
            if(!IsLockModule(nMods-i, j)){
                bits.push_back(PixelValue(nMods-i, j));
                n++;
            }
            if(!IsLockModule(nMods-i-1, j)){
                bits.push_back(PixelValue(nMods-i-1, j));
                n++;
            }
        }
    }

    //中间区域
    for(int i = 1; i <= nMods-17; i += 2){
        //向上填充
        for(int j = 1; j <= nMods; j++){
            if(!IsLockModule(nMods-8-i, nMods-j)){
                bits.push_back(PixelValue(nMods-8-i, nMods-j));
                n++;
            }
            if(!IsLockModule(nMods-9-i, nMods-j)){
                bits.push_back(PixelValue(nMods-9-i, nMods-j));
                n++;
            }
        }
        //向下填充
        i += 2;
        for(int j = 0; j <= nMods-1; j++){
            if(!IsLockModule(nMods-8-i, j)){
                bits.push_back(PixelValue(nMods-8-i, j));
                n++;
            }
            if(!IsLockModule(nMods-9-i, j)){
                bits.push_back(PixelValue(nMods-9-i, j));
                n++;
            }
        }
    }

    //左侧区域1
    for(int i = 1; i <= nMods-17; i++){
        if(!IsLockModule(8, nMods-8-i)){
            bits.push_back(PixelValue(8, nMods-8-i));
            n++;
        }
        if(!IsLockModule(7, nMods-8-i)){
            bits.push_back(PixelValue(7, nMods-8-i));
            n++;
        }
    }
    //左侧区域2
    for(int i = 9; i < nMods-8; i++){
        if(!IsLockModule(5, i)){
            bits.push_back(PixelValue(5, i));
            n++;
        }
        if(!IsLockModule(4, i)){
            bits.push_back(PixelValue(4, i));
            n++;
        }
    }
    //左侧区域3
    for(int i = 1; i <= nMods-17; i++){
        if(!IsLockModule(3, nMods-8-i)){
            bits.push_back(PixelValue(3, nMods-8-i));
            n++;
        }
        if(!IsLockModule(2, nMods-8-i)){
            bits.push_back(PixelValue(2, nMods-8-i));
            n++;
        }
    }
    //左侧区域4
    for(int i = 9; i < nMods-8; i++){
        if(!IsLockModule(1, i)){
            bits.push_back(PixelValue(1, i));
            n++;
        }
        if(!IsLockModule(0, i)){
            bits.push_back(PixelValue(0, i));
            n++;
        }
    }

    StringUtils::PRINT("读取数据长度: ", n);

    return bits;
}

std::pair<std::vector<int>, std::vector<int> > ImageReader::ReadFormatBits()
{
    //1.Add the Format Information Area 1
    std::vector<int> f1;
    f1.push_back(PixelValue(8, nMods-1));
    f1.push_back(PixelValue(8, nMods-2));
    f1.push_back(PixelValue(8, nMods-3));
    f1.push_back(PixelValue(8, nMods-4));
    f1.push_back(PixelValue(8, nMods-5));
    f1.push_back(PixelValue(8, nMods-6));
    f1.push_back(PixelValue(8, nMods-7));
    f1.push_back(PixelValue(nMods-8, 8));
    f1.push_back(PixelValue(nMods-7, 8));
    f1.push_back(PixelValue(nMods-6, 8));
    f1.push_back(PixelValue(nMods-5, 8));
    f1.push_back(PixelValue(nMods-4, 8));
    f1.push_back(PixelValue(nMods-3, 8));
    f1.push_back(PixelValue(nMods-2, 8));
    f1.push_back(PixelValue(nMods-1, 8));

    //2.Add the Format Information Area 2
    std::vector<int> f2;
    f2.push_back(PixelValue(0, 8));
    f2.push_back(PixelValue(1, 8));
    f2.push_back(PixelValue(2, 8));
    f2.push_back(PixelValue(3, 8));
    f2.push_back(PixelValue(4, 8));
    f2.push_back(PixelValue(5, 8));
    f2.push_back(PixelValue(7, 8));
    f2.push_back(PixelValue(8, 8));
    f2.push_back(PixelValue(8, 7));
    f2.push_back(PixelValue(8, 5));
    f2.push_back(PixelValue(8, 4));
    f2.push_back(PixelValue(8, 3));
    f2.push_back(PixelValue(8, 2));
    f2.push_back(PixelValue(8, 1));
    f2.push_back(PixelValue(8, 0));

    StringUtils::PRINT("格式信息数据位流：", f1);
    return std::make_pair(f1, f2);
}

std::pair<std::vector<int>, std::vector<int> > ImageReader::ReadVersionBits()
{
    std::vector<int> f1;
    std::vector<int> f2;
    for(int i = 5; i >= 0; i--){
        f1.push_back(PixelValue(i, nMods-9));
        f1.push_back(PixelValue(i, nMods-10));
        f1.push_back(PixelValue(i, nMods-11));

        f2.push_back(PixelValue(nMods-9, i));
        f2.push_back(PixelValue(nMods-10, i));
        f2.push_back(PixelValue(nMods-11, i));
    }

    return std::make_pair(f1, f2);
}

int ImageReader::PixelValue(int x, int y)
{
    if(x < image.width() && y < image.height()){
        QColor color = image.pixelColor(x, y);
        if(color == value_b){
            return 1;
        } else if(color == value_w){
            return 0;
        } else {
            qDebug() << "imagereader.cpp PixelValue(): 像素颜色未识别";
        }
    } else {
        qDebug() << "imagereader.cpp PixelValue(): 超出范围";
    }
    return 0;
}

bool ImageReader::IsLockModule(int x, int y)
{
    //功能模块
    if(x >= 0 && x <= 7 && y >= 0 && y <= 7){
        return true;
    }
    if(x >= 0 && x <= 7 && y >= (nMods-8) && y <= (nMods-1)){
        return true;
    }
    if(x >= (nMods-8) && x <= (nMods-1) && y >= 0 && y <= 7){
        return true;
    }


    //定位图形、矫正图形、格式信息、版本信息模块
    auto result = std::find(lockModules.begin(), lockModules.end(),
                            std::make_pair(x, y));
    if(result != lockModules.end()){
        return true;
    }

    return false;

}

void ImageReader::setImage(const QImage &value)
{
    image = value;
}

std::vector<int> ImageReader::getDatabitstream() const
{
    return databitstream;
}

std::pair<std::vector<int>, std::vector<int> > ImageReader::getFormatbits() const
{
    return formatbits;
}

std::pair<std::vector<int>, std::vector<int> > ImageReader::getVersionbits() const
{
    return versionbits;
}

int ImageReader::getVersion() const
{
    return version;
}

}
